
package com.chuanke.ckfamily.action.user;

import com.alibaba.fastjson.JSON;
import com.chuanke.ckfamily.action.BaseController;
import com.chuanke.ckfamily.action.base.Response;
import com.chuanke.ckfamily.dao.user.model.User;
import com.chuanke.ckfamily.service.base.FailCode;
import com.chuanke.ckfamily.service.common.CacheService;
import com.chuanke.ckfamily.service.common.WyService;
import com.chuanke.ckfamily.service.common.impl.RedisLockUtil;
import com.chuanke.ckfamily.service.exception.CFException;
import com.chuanke.ckfamily.service.user.ChanagedFriends;
import com.chuanke.ckfamily.service.user.FriendApplyService;
import com.chuanke.ckfamily.service.user.UserRelationService;
import com.chuanke.ckfamily.service.user.UserService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/api/friend")
public class FriendController extends BaseController {
	private static final Logger log = LoggerFactory.getLogger(FriendController.class);

	@Autowired
	private UserService userService;

	@Autowired
	private UserRelationService userRelationService;

	@Autowired
	private FriendApplyService friendApplyService;

	@Autowired
	private CacheService cacheService;

	@Autowired
	private WyService wyService;

	@ResponseBody
	@RequestMapping("/get_hi")
	public Response getHi(HttpServletRequest request, String targer_no) {
		try {
			Long uid = getUidByToken(request);
			return new Response(friendApplyService.getApplyHi(uid, targer_no));

		} catch (Exception e) {
			e.printStackTrace();
			return new Response(FailCode.APP, e.getMessage());

		}
	}

	/**
	 * <h1>获取用户好友</h1>
	 * 
	 * /api/friend/myFriend
	 * 
	 * @param date
	 *            日期
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/my_friend")
	public Response myFriend(HttpServletRequest request, @RequestParam(value = "date", required = false) String date) {
		try {
			Long uid = getUidByToken(request);
			log.debug("my_friend=============>begin uid={},  date={}", uid, date);

			ChanagedFriends chanagedFriends = userRelationService.getFriends(uid, date);
			log.debug("my_friend==============>end {}", JSON.toJSON(chanagedFriends));
			return new Response(chanagedFriends);

		} catch (CFException e) {
			e.printStackTrace();
			return new Response(FailCode.APP, e.getMessage());

		} catch (Exception e) {
			e.printStackTrace();
			return new Response(e);

		}
	}

	/**
	 * <h1>申请用户好友</h1>
	 * 
	 * /api/friend/apply
	 * 
	 * @param request
	 * @param userNo 被申请userNo
	 * @param hi 	  打招呼
	 * @param friendRemark 备注
	 * @return
	 */

	@ResponseBody
	@RequestMapping("/apply")
	public Response applyFriend(HttpServletRequest request, @RequestParam(value = "user_no") String userNo, @RequestParam(value = "hi", required = false) String hi, @RequestParam(value = "remark_name", required = false) String friendRemark, @RequestParam(value = "friend_remark_message", required = false) String friend_remark_message) {
		// log.debug("api friend apply ------------------->userNo={} ",userNo );
		RedisLockUtil lockUtil = null;
		try {
			Long requester = getUidByToken(request);
			Long responser = userService.findByUserNo(userNo).getId();
			lockUtil = cacheService.getLock(CacheService.KEY_PREFIX.friend_apply_.name() + Math.min(requester, responser) + "_" + Math.max(requester, responser));
			friendApplyService.applyFriend(requester, userNo, hi, friendRemark);
			log.debug("申请好友结束-------------->");
			return new Response("申请成功");

		} catch (CFException e) {
			e.printStackTrace();
			return new Response(e);

		} catch (Exception e) {
			e.printStackTrace();
			return new Response(FailCode.APP, e.getMessage());

		} finally {
			if (lockUtil != null) {
				lockUtil.unlock();
			}
		}
	}

	/**
	 * <h1>获取被申请列表</h1>
	 * 
	 * /api/friend/beApply
	 * 
	 * @param
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/be_apply")
	public Response beApply(HttpServletRequest request) {
		try {
			Long uid = getUidByToken(request);
			log.debug("=====================>uid={}", uid);
			return new Response(friendApplyService.getFriendApplyRecord(uid));
		} catch (CFException e) {
			e.printStackTrace();
			return new Response(e);

		} catch (Exception e) {
			e.printStackTrace();
			return new Response(FailCode.APP, e.getMessage());
		}
	}

	/**
	 * <h1>审核好友申请</h1>
	 * 
	 * /api/friend/audit_apply
	 * 
	 * @param aid 申请列表id
	 * @param op 操作类型 2同意好友申请 4删除好友申请
	 * @return
	 */

	@ResponseBody
	@RequestMapping("/audit_apply")
	public Response auditApply(HttpServletRequest request, @RequestParam(value = "aid") Long aid, @RequestParam(value = "op") int op) {
		try {
			Long uid = getUidByToken(request);
			friendApplyService.auditApply(uid, aid, op);
			return new Response("申请成功");
		} catch (CFException e) {
			e.printStackTrace();
			return new Response(e);

		} catch (Exception e) {
			e.printStackTrace();
			return new Response(FailCode.APP, e.getMessage());
		}
	}

	/**
	 * <h1>删除好友</h1>
	 * 
	 * /api/friend/delete_friend
	 * 
	 * @param tuid 被删除人
	 * @return
	 */

	@ResponseBody
	@RequestMapping("/delete_friend")
	public Response deleteFriend(HttpServletRequest request, @RequestParam(value = "tuid") Long tuid) {
		RedisLockUtil lockUtil = null;

		try {
			Long uid = getUidByToken(request);
			lockUtil = cacheService.getLock(CacheService.KEY_PREFIX.friend_delete_.name() + Math.min(tuid, uid) + "_" + Math.max(tuid, uid));
			userRelationService.deleteFriend(uid, tuid);
			log.debug("/delete_friend =================>删除好友 uid={}, tuid={}", uid, tuid);
			return new Response("删除成功");
			
		} catch (CFException e) {
			e.printStackTrace();
			return new Response(e);

		} catch (Exception e) {
			e.printStackTrace();
			return new Response(FailCode.APP, e.getMessage());
			
		} finally {
			if (lockUtil != null) {
				lockUtil.unlock();
			}

		}
	}

}
